package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.UserApexInfoMapper;
import cc.rengu.igas.bomp.common.dao.UserInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.UserApexInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.UserInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.UserApexInfo;
import cc.rengu.igas.bomp.common.entity.UserInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.UserApexInfoBean;
import cc.rengu.igas.bomp.facade.bean.UserInfoBean;
import cc.rengu.igas.bomp.facade.request.UserInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.UserInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangcheng on 2021/7/19 15:27
 */
public class UserInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        UserInfoQueryRequest userInfoQueryRequest = new UserInfoQueryRequest();
        ConvertUtil.convertOutput(userInfoQueryRequest);
        return userInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        UserInfoQueryRequest userInfoQueryRequest = (UserInfoQueryRequest) request;
        String instId = userInfoQueryRequest.getHeader().getInstId();
        String userId = userInfoQueryRequest.getUserId();
        String userInst = userInfoQueryRequest.getUserInst();
        String certType = userInfoQueryRequest.getCertType();
        String certNo = userInfoQueryRequest.getCertNo();
        String phoneNo = userInfoQueryRequest.getPhoneNo();
        String userType;
        if (ChannelEnum.UPMP.getChannelType().equals(userInfoQueryRequest.getHeader().getChanlId())) {
            userType = "00";
        } else if (ChannelEnum.BSPS.getChannelType().equals(userInfoQueryRequest.getHeader().getChanlId())) {
            userType = "01";
        } else if (ChannelEnum.OPMS.getChannelType().equals(userInfoQueryRequest.getHeader().getChanlId())) {
            userType = "02";
        } else {
            userType = "03";
        }
        UserInfoMapper userInfoMapper = new UserInfoMapperImpl();
        List<UserInfo> userInfoList = new ArrayList<>();
        if ("00".equals(userInfoQueryRequest.getQueryType())) {
            if (StringUtil.isEmptyOrNull(userInfoQueryRequest.getUserId())) {
                rglog.error("用户标识为空:<{}>", userInfoQueryRequest.getUserId());
                throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
            }
            UserInfo userInfo = userInfoMapper.selectUserInfoByPrimaryKey(instId, userId);
            if (null != userInfo) {
                userInfoList.add(userInfo);
            }
        } else if ("01".equals(userInfoQueryRequest.getQueryType())) {
            if (StringUtil.isEmptyOrNull(userInfoQueryRequest.getUserInst()) || StringUtil.isEmptyOrNull(userInfoQueryRequest.getCertType()) || StringUtil.isEmptyOrNull(userInfoQueryRequest.getCertNo())) {
                rglog.error("用户所属主体:<{}>或证件类型:<{}>或证件号码:<{}>为空", userInfoQueryRequest.getUserInst(), userInfoQueryRequest.getCertType(), userInfoQueryRequest.getCertNo());
                throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
            }
            UserInfo userInfo = userInfoMapper.selectUserInfoByCertInfo(instId, userType, userInst, certType, certNo);
            if (null != userInfo) {
                userInfoList.add(userInfo);
            }
        } else if ("02".equals(userInfoQueryRequest.getQueryType())) {
            if (StringUtil.isEmptyOrNull(userInfoQueryRequest.getUserInst()) || StringUtil.isEmptyOrNull(userInfoQueryRequest.getPhoneNo())) {
                rglog.error("用户所属主体:<{}>或手机号码:<{}>为空", userInfoQueryRequest.getUserInst(), userInfoQueryRequest.getPhoneNo());
                throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
            }
            List<UserInfo> userInfos = userInfoMapper.selectUserInfoByPhoneNo(instId, userType, userInst, phoneNo);
            if (null != userInfos && !userInfos.isEmpty()) {
                userInfoList.addAll(userInfos);
            }
        } else if ("03".equals(userInfoQueryRequest.getQueryType())) {
            if (StringUtil.isEmptyOrNull(userInfoQueryRequest.getOpenId())) {
                rglog.error("用户openId:<{}>为空", userInfoQueryRequest.getOpenId());
                throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
            }
            UserInfo userInfo = userInfoMapper.selectUserInfoByOpenId(instId, userType, userInfoQueryRequest.getOpenId());
            if (null != userInfo) {
                userInfoList.add(userInfo);
            }
        } else if ("04".equals(userInfoQueryRequest.getQueryType())) {
            if (StringUtil.isEmptyOrNull(userInfoQueryRequest.getLoginId())) {
                rglog.error("用户登录账号:<{}>为空", userInfoQueryRequest.getLoginId());
                throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
            }
            UserInfo userInfo = userInfoMapper.selectUserInfoByLoginId(instId, userType, userInfoQueryRequest.getLoginId());
            if (null != userInfo) {
                userInfoList.add(userInfo);
            }
        } else {
            rglog.error("不支持的用户信息查询方式:<{}>", userInfoQueryRequest.getQueryType());
            throw new BizException(RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespCode(), RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespDesc());
        }

        /* 组装应答报文 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        UserInfoQueryResponse userInfoQueryResponse = new UserInfoQueryResponse();
        userInfoQueryResponse.setHeader(userInfoQueryRequest.getHeader());
        List<UserInfoBean> userInfoBeanList = new ArrayList<>();
        if (!userInfoList.isEmpty()) {
            for (UserInfo userInfo : userInfoList) {
                convUserInfo(userInfo, userInfoBeanList);
            }
            userInfoQueryResponse.setUserInfoList(userInfoBeanList);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            bizResponse.setRespCode(RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.QUERY_AUTH_USER_IS_NULL.getRespDesc());
        }
        bizResponse.setResult(userInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        UserInfoQueryResponse userInfoQueryResponse = (UserInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(userInfoQueryResponse);
    }

    /**
     * 数据库用户信息转换成接口用户信息
     *
     * @param userInfo         数据库用户信息
     * @param userInfoBeanList 接口返回的用户信息
     * @throws Exception 异常
     */
    private void convUserInfo(UserInfo userInfo, List<UserInfoBean> userInfoBeanList) throws Exception {
        UserInfoBean userInfoBean = new UserInfoBean();
        BeanUtil.beanCopy(userInfo, userInfoBean);
        UserApexInfoMapper userApexInfoMapper = new UserApexInfoMapperImpl();
        List<UserApexInfo> userApexInfoList = userApexInfoMapper.selectUserApexInfoByUserId(userInfo.getInstId(), userInfo.getUserId());
        if (null != userApexInfoList && !userApexInfoList.isEmpty()) {
            for (UserApexInfo userApexInfo : userApexInfoList) {
                UserApexInfoBean userApexInfoBean = new UserApexInfoBean();
                BeanUtil.beanCopy(userApexInfo, userApexInfoBean);
                userInfoBean.getUserApexInfoList().add(userApexInfoBean);
            }
        }
        userInfoBeanList.add(userInfoBean);
    }
}
